import 'package:flutter/cupertino.dart';
import 'package:scoped_model/scoped_model.dart';

import '../../utils/shared_preferences.dart';

class AuthModel extends Model{

  String _token = '';

  String _name = '';


  String get name => _name;

  String get token => _token;

  bool get isLogin => _token != '';

  initApp(BuildContext context) async{
    try {
      String tocken = await SharedPreferencesUtils.getInstance().getString('token');
      String username = await SharedPreferencesUtils.getInstance().getString('username');
      login(tocken, username,context);
      Navigator.of(context).pushReplacementNamed('/');
    }catch(e){
      Navigator.of(context).pushReplacementNamed('/login');
    }
  }

  void login(String token,String name,BuildContext context) {
    _token = token;
    _name = name;
    notifyListeners();
  }

  void logout() async{
    await SharedPreferencesUtils.getInstance().setString('token','');
    await SharedPreferencesUtils.getInstance().setString('username','');
    _token = '';
    notifyListeners();
  }
}